<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport"
			content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
		<style type="text/css">
			html,
			body,
			.canvas {
				padding: 0;
				margin: 0;
				overflow-y: hidden;
				background-color: transparent;
				width: 100%;
				height: 100%;
			}

			/* .canvas,
			textarea {
				width: 100%;
				height: 200px;
			} */
		</style>
	</head>
	<body>
		<div class="canvas" id="limeChart"></div>
		<script type="text/javascript" src="./js/uni.webview.1.5.2.js">
		</script>
		<script type="text/javascript" src="./js/echarts.min.js"></script>
		<script type="text/javascript" src="./js/dist/ecStat.min.js">
		</script>
		<script type="text/javascript">
			// version: '0.2.7',
			let chart = null;
			let cache = [];
			console.log = function(...agrs) {
				postMessage(agrs)
			}

			function appendChild(str) {
				document.body.appendChild(document.createRange().createContextualFragment(`
					<textarea>${str}</textarea>
				`))
			}

			function stringify(key, value) {
				if (typeof value === 'object' && value !== null) {
					if (cache.indexOf(value) !== -1) {
						return;
					}
					cache.push(value);
				}
				return value;
			}

			function emit(event, data) {
				let dataStr = JSON.stringify(data, stringify)
				postMessage({
					event,
					data: dataStr
				})
				cache = []
			}

			function postMessage(data) {
				uni.postMessage({
					data
				});
			}

			function isEmpty(data, msg) {
				if (!data || JSON.stringify(data) == '{}') {
					postMessage({
						error: msg
					})
					return true
				}
				return false
			}

			function setName(name, data, type) {
				const names = `${type}Names`
				if (!window[names]) {
					window[names] = [{
						key: name,
						name: data
					}]
				} else {
					window[names].push({
						key: name,
						name: data
					})
				}
			}

			function getNameStr(type) {
				const names = `${type}Names`
				const str = ``
				if (!window[names]) {
					return ''
				}
				return window[names].map(v => `var ${v.name} = limeparams.${v.key}`).join(';') + ';'
			}

			function getNameParams(data, type) {
				const names = `${type}Names`;
				if (!window[names]) {
					return data
				}
				window[names].forEach(v => {
					data[v.key] = data[v.key] || data.params[v.key] || eval(v.key)
				})
				return data
			}

			function parseStr(callbackStr, params, type) {
				const config = callbackStr.match(/^function(\s+)?\((\w+)\)(\s+)?\{/);
				if (!config) return
				let newstr = `${callbackStr}`
				let paramStr = `var params = limeparams.params;`
				paramStr += (type == 'init' ? 'var config = limeparams.config;' : '')
				try {
					// 当直接返回的是函数字符串时
					return paramStr + eval(`var a = ${callbackStr};a()`)
				} catch (e) {
					// config
					if (type == 'init') {
						setName('config', config[2], type)
						const echartsNameReg = /(var\s+)?([\w]+)(\s+)?\=(\s+)?([\w]+)\.init\([\w,\s]+\)/
						const echartsName = newstr.match(echartsNameReg)
						setName('chart', echartsName[2], type)
						setName('echarts', echartsName[5], type)
						newstr.replace(echartsNameReg, '')
					} else {
						setName('chart', config[2], type)
					}
					// option 
					const optionNameReg = /[\w]+\.setOption\(([\w]+\.)?([\w]+)\)/
					if (optionNameReg.test(newstr)) {
						const optionNames = newstr.match(optionNameReg)
						if (optionNames[1]) {
							setName('params', optionNames[1].split('.')[0], type)
						} else {
							const paramsReg = new RegExp(`(var\\s+)?${optionNames[2]}(\\s+)?\\=(\\s+)?\\{`)
							if (!paramsReg.test(newstr)) {
								// series
								const index = Object.values(params).findIndex(v => v.series)
								setName(Object.keys(params)[index] || optionNames[2], optionNames[2], type)
							}
						}
					}
					// ecStat
					const ecStatNameReg = /registerTransform\(([\w\.]+)\)/
					if (ecStatNameReg.test(newstr)) {
						let name = newstr.match(ecStatNameReg)[1]
						if (name.includes('.default')) {
							const names = name.split('.default')
							newstr = newstr.replace(`${name}`, `ecStat${names[1]}`)
							name = names[0]
						}
						setName('ecStat', name, type)
					}
					// registerMap
					const mapNameReg = /([\w]+)\.registerMap\((\s+)?([\w'"]+)(\s+)?,(\s+)?([\w]+)/
					if (mapNameReg.test(newstr)) {
						const names = newstr.match(mapNameReg)
						setName('echarts', names[1], type)
						// FeatureCollection
						const index = Object.values(params).findIndex(v => v.type == "FeatureCollection")
						setName(Object.keys(params)[index] || names[6], names[6], type)
					}
					paramStr += getNameStr(type)
					return paramStr + (newstr
						.replace(/^function[\s\w\n\(,\)]+\{/, '')
						.replace(/(.*)\}$/, '$1')
						.replace(/([\w]+)\("log",/g, 'console.log('))
				}
			}
			// init()
			function init(callbackStr, params = {}) {
				const canvas = document.getElementById('limeChart')
				canvas.setChart = () => {}
				const config = {
					canvas,
					devicePixelRatio: window.devicePixelRatio,
					// width: canvas.offsetWidth,
					// height: canvas.offsetHeight,
					width: window.innerWidth,
					height: window.innerHeight,
				}
				if (!callbackStr) {
					chart = echarts.init(canvas, null, config)
					emit('inited', true)
				} else {
					try {
						const chartStr = parseStr(callbackStr, params, 'init')
						const limeparams = getNameParams({
							config,
							params
						}, 'init')
						for (let key in params) {
							window[key] = params[key]
						}
						const chartFunc = new Function('limeparams', chartStr);
						chart = chartFunc(limeparams)
						chart.$emit = emit
						emit('inited', true)
					} catch (e) {
						console.log(e)
					}
				}
			}

			function setChart(callbackStr, params = {}) {
				isEmpty(params, 'params: 数据为空')
				if (chart) {
					try {
						appendChild(callbackStr)
						const chartStr = parseStr(callbackStr, params, 'setChart')
						appendChild(chartStr)
						const limeparams = getNameParams({
							params
						}, 'setChart')
						for (let key in params) {
							window[key] = params[key]
						}
						const chartFunc = new Function('limeparams', chartStr);
						chartFunc(limeparams)
					} catch (e) {
						console.log(e)
					}
				} else {
					console.log('组件未初始化')
				}
			}

			function setOption(data) {
				if (chart) chart.setOption(data)
			}

			function showLoading() {
				if (chart) chart.showLoading()
			}

			function hideLoading() {
				if (chart) chart.hideLoading()
			}

			function clear() {
				if (chart) chart.clear()

			}

			function dispose() {
				if (chart) chart.dispose()
			}

			function canvasToTempFilePath(opt = {}) {
				if (chart) {
					const src = chart.getDataURL(opt)
					postMessage({
						file: true,
						data: src
					})
				}
			}
		</script>


	</body>
</html>
<!-- saved from url=(0081)http://liangei.gitee.io/limeui/hybrid/html/lime-ui/lime-echart/index.html?v=0.2.3 -->